package cevent.source.cloudcenter.business.controller.admin;

import cevent.source.cloudcenter.server.dto.GuestBookDto;
import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.dto.PageSelectDto;
import cevent.source.cloudcenter.server.dto.ResponseDataDto;
import cevent.source.cloudcenter.server.service.GuestBookService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author cevent
 * @description 留言管理表控制层
 */
@RestController
@RequestMapping("/admin/guestBook")
public class GuestBookController {
    private static final Logger LOG = LoggerFactory.getLogger(GuestBookController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="模块管理 留言管理表";
    @Resource
    private GuestBookService guestBookService;

    //1.dto-guestBook获取
    @RequestMapping("/dtoList")
    public List<GuestBookDto> getDtoList() {
        return guestBookService.guestBookDtoList();
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody PageDto pageDto) {
        LOG.info("查看controller的pageDto：{}", pageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        guestBookService.getGuestBookPageDtoList(pageDto);
        responseData.setResponseData(pageDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody GuestBookDto guestBookDto) {
        LOG.info("传入的guestBook DTO：{}", guestBookDto);

        ValidatorUtil.requiredEmpty(guestBookDto.getMemberId(), "会员ID");
        ValidatorUtil.requiredEmpty(guestBookDto.getEmployeeId(), "员工ID");
        ResponseDataDto responseData = new ResponseDataDto();
        guestBookService.save(guestBookDto);
        responseData.setResponseData(guestBookDto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的guestBook uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        guestBookService.delete(uniId);
        return responseData;
    }

    @DeleteMapping("/delBatch/{uniId}")
    public ResponseDataDto delBatch(@PathVariable String uniId) {
        LOG.info("传入批量删除的guestBook uniId（父ID）：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        guestBookService.deleteBatch(uniId);
        return responseData;
    }
    //根据reply进行回复状态分页查询
    @PostMapping("/replyPage")
    public ResponseDataDto getReplyPage(@RequestBody PageSelectDto pageSelectDto){
        ResponseDataDto responseData=new ResponseDataDto();
        if(pageSelectDto.getTypeId()==null){
            guestBookService.getGuestBookPageDtoList(pageSelectDto);
        }else{
            guestBookService.getGuestBookPageSelectReply(pageSelectDto);
        }

        responseData.setResponseData(pageSelectDto);
        return responseData;
    }
    @GetMapping("/replyCount")
    public ResponseDataDto getReplyCount(){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(guestBookService.getReplyCount());
        return responseData;
    }

    //根据uniId查询parentId=uniId的子节点数据
    @GetMapping("/childList/{uniId}")
    public ResponseDataDto getGuestBookChilList(@PathVariable String uniId){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(guestBookService.getGuestBookChildList(uniId));
        return responseData;
    }

    //分页带父子结构tree数据
    @PostMapping("/pageTreeList")
    public ResponseDataDto guestBookPageList(@RequestBody PageSelectDto pageSelectDto){
        ResponseDataDto responseData=new ResponseDataDto();
        guestBookService.guestBookPageList(pageSelectDto);
        responseData.setResponseData(pageSelectDto);
        return responseData;
    }
}
